﻿using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Documents;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 将当前子面板中的所有格式化为“标题”文本块及正文文本块的文本内容复制成富文本块格式的提纲。
        /// </summary>
        public static void CopyAsOutLine()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null)
            {
                MessageBox.Show("　　请先选中一个公式来导出提纲。选中普通公式会导出父面板中所有文本块构成的提纲。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            LunarPresentation.Expressions.SubPanel curSubPanel = curEx as LunarPresentation.Expressions.SubPanel;
            if (curSubPanel == null)
            {
                curSubPanel = curEx.ParentPanel;
            }

            if (curSubPanel == null)
            {
                MessageBox.Show("发生意外，未找到要导出提纲的子面板。",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Dialogs.OutLineDialog oDlg = new Dialogs.OutLineDialog(Globals.MainWindow);

            if (curSubPanel.Editor.DefaultBackground == Brushes.Black)
            {
                oDlg.MainRTB.Background = Brushes.White;//如果是黑底白字，自动反色。
            }
            else
            {
                oDlg.MainRTB.Background = curSubPanel.Editor.DefaultBackground;
            }

            foreach (Expressions.Expression ex in curSubPanel.BasePanel.Children)
            {
                Expressions.BaseText bt = ex as Expressions.BaseText;
                if (bt == null) continue;

                Paragraph newParagraph;

                string autoNumString = bt.AutoNumString;
                if (autoNumString != null && autoNumString != "")
                {
                    newParagraph = new Paragraph(new Span(new Run(autoNumString + bt.Text)));
                }
                else
                {
                    newParagraph = new Paragraph(new Span(new Run(bt.Text)));
                }

                newParagraph.FontFamily = bt.TextBlock.FontFamily;
                newParagraph.FontSize = bt.TextBlock.FontSize;
                newParagraph.TextIndent = bt.Indent * bt.TextBlock.FontSize * 2;
                newParagraph.TextDecorations = bt.TextBlock.TextDecorations;
                newParagraph.FontWeight = bt.TextBlock.FontWeight;
                newParagraph.FontStyle = bt.TextBlock.FontStyle;

                if (bt.TextBlock.Foreground == Brushes.White)
                {
                    newParagraph.Foreground = Brushes.Black;//黑白反色。
                }
                else
                {
                    newParagraph.Foreground = bt.TextBlock.Foreground;
                }

                if (bt.TextBlock.Background == Brushes.Black)
                {
                    newParagraph.Background = Brushes.White;
                }
                else
                {
                    newParagraph.Background = bt.TextBlock.Background;
                }

                switch (bt.Align)
                {
                    case Expressions.Expression.Alignment.Center:
                        {
                            newParagraph.TextAlignment = TextAlignment.Center; break;
                        }
                    case Expressions.Expression.Alignment.Default:
                        {
                            switch (curSubPanel.Align)
                            {
                                case Expressions.Expression.Alignment.Center:
                                    {
                                        newParagraph.TextAlignment = TextAlignment.Center; break;
                                    }
                                case Expressions.Expression.Alignment.Default:
                                case Expressions.Expression.Alignment.LeftOrTop:
                                    {
                                        newParagraph.TextAlignment = TextAlignment.Left; break;
                                    }
                                case Expressions.Expression.Alignment.RightOrBottom:
                                    {
                                        newParagraph.TextAlignment = TextAlignment.Right; break;
                                    }
                            }
                            break;
                        }
                    case Expressions.Expression.Alignment.LeftOrTop:
                        {
                            newParagraph.TextAlignment = TextAlignment.Left; break;
                        }
                    case Expressions.Expression.Alignment.RightOrBottom:
                        {
                            newParagraph.TextAlignment = TextAlignment.Right; break;
                        }
                }

                oDlg.AddParagraph(newParagraph);
            }

            oDlg.ShowDialog();
        }
    }
}
